/**
 * chinagoods.com
 * Copyright (c) 2020-2024 All Rights Reserved.
 */
package com.ruoyi.web.controller.bot;

import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.GetFile;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
import org.telegram.telegrambots.meta.api.objects.*;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * @author yangpeng
 * Created by on 2024-03-26 11:27
 */


@Data
@Slf4j
public class YpTestBot extends TelegramLongPollingBot {
    //填你自己的token和username
    // YpTestBot("yp1989test_bot", "6421582328:AAH2fkMrpd4OoNvXvKjBMZHwJk_D_JUtImk", "yp机器人"),


    private String token = "6421582328:AAH2fkMrpd4OoNvXvKjBMZHwJk_D_JUtImk";
    private String botUsername = "yp1989test_bot";

//    public YpTestBot(DefaultBotOptions options) {
//        super(options);
//
//    }

    public YpTestBot() {
        super(new DefaultBotOptions(), "6421582328:AAH2fkMrpd4OoNvXvKjBMZHwJk_D_JUtImk");
    }


    /**
     * 监听机器人的消息
     */
    @SneakyThrows
    @Override
    public void onUpdateReceived(Update update) {
        String result = "";


        try {

            if (update.hasMessage()) {

                Message message = update.getMessage();
                log.info("消息json:{}", JSONUtil.toJsonStr(message));
                Long chatId = message.getChatId();
                String text = message.getText();
                List<PhotoSize> photo = message.getPhoto();
                PhotoSize photoSize=photo.get(0);
                GetFile get=new GetFile();
                get.setFileId(photoSize.getFileId());
                File file = this.execute(get);
                SendPhoto sendPhoto=new SendPhoto();
                sendPhoto.setChatId(chatId);
                String fileUrl = file.getFileUrl(this.getToken());
                URL url=new URL(fileUrl);
                HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpConn.getInputStream();
               // sendPhoto.setPhoto(inputStream);
                this.execute(sendPhoto);

                log.info("指令为：{}", text);
                User from = message.getFrom();
                this.sendTextMsg("你这个老6" + message.getText(), chatId.toString());

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }



    /**
     * 发送文本消息
     *
     * @param text   内容
     * @param chatId 内容ID
     */
    @SneakyThrows
    @Async
    public void sendTextMsg(String text, String chatId) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setDisableNotification(false);
        sendMessage.setChatId(chatId);
        sendMessage.setText(text);
        executeAsync(sendMessage);
    }


    @SneakyThrows
    @Async
    public void sendPhotoMsg(String text, String chatId) {
        SendPhoto sendMessage = new SendPhoto();
        sendMessage.setDisableNotification(false);
        sendMessage.setChatId(chatId);

        executeAsync(sendMessage);
    }


}

